package com.e.takeaway.framgent;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.e.takeaway.R;
import com.e.takeaway.adapter.OrderAdapter;
import com.e.takeaway.po.MerChant;
import com.e.takeaway.po.Order;
import com.e.takeaway.service.RequestService;
import com.e.takeaway.util.MyRetrofit;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * A simple {@link Fragment} subclass.

 * create an instance of this fragment.
 */
public class OrderFramgent extends Fragment {
    private static final String FILE_NAME = "user";
    private ListView listView;
    private List<Order> orders;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_order, container, false);
        System.out.println("进入了order页面");
        listView = (ListView) v.findViewById(R.id.order_list1);
        SharedPreferences sp =getActivity().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);

        System.out.println("进入了order页面");
        String phone = sp.getString("phone","");
        System.out.println("++++++++++++++orderphone"+phone);
        setData(phone);

        return v;
    }

    private void setData(String phone){
        Retrofit retrofit = MyRetrofit.getRetrofit();
        RequestService request = retrofit.create(RequestService.class);
        Call<List<Order>> call = request.getOrderList(phone);
        call.enqueue(new Callback<List<Order>>() {
            @Override
            public void onResponse(Call<List<Order>> call, Response<List<Order>> response) {
                orders = response.body();
                // System.out.println("tttttttttttttttttttttttttttttt: " + orders);
                if (null != orders && orders.size()>0){
                    OrderAdapter orderAdapter = new OrderAdapter(getActivity(),orders);
                    listView.setAdapter(orderAdapter);
                }
            }

            @Override
            public void onFailure(Call<List<Order>> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }
}